package com.lkd.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lkd.entity.Node;
import com.lkd.service.NodeService;
import com.lkd.vo.NodeVo;
import com.lkd.vo.PageBean;
import com.lkd.vo.Result;
import com.lkd.vo.VendingMachineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 点位表 前端控制器
 * </p>
 *
 * @author 聂德祥
 * @since 2024-08-19
 */
@RestController
public class NodeController {
    @Autowired
    private NodeService nodeService;
    @GetMapping("/api/vm-service/node/search")
    public Result search(Integer regionId,String name, @RequestParam(defaultValue = "1") Integer pageIndex, @RequestParam(defaultValue = "10") Integer pageSize){
        PageBean<NodeVo> page = nodeService.search(regionId,name,pageIndex,pageSize);
        return Result.success(page);
    }
    // 新增点位
    @PostMapping("/api/vm-service/node")
    public Result add(@RequestBody Node node){
        nodeService.save(node);
        return Result.success();
    }
    // 获取点位详情
    @GetMapping("/api/vm-service/node/vmList/{nodeId}")
    public Result getById(@PathVariable Long nodeId){
       List<VendingMachineVo> vendingMachineVos  =  nodeService.findVMById(nodeId);
        return Result.success(vendingMachineVos);
    }
    @PutMapping("/api/vm-service/node")
    public Result update(@RequestBody Node node){
        nodeService.updateById(node);
        return Result.success();
    }
    @DeleteMapping("/api/vm-service/node/{nodeId}")
    public Result delete(@PathVariable Long nodeId){
        // TODO 删除点位之前，判断该点位是否已经绑定了售货机
        nodeService.removeById(nodeId);
        return Result.success();
    }

    @GetMapping("/api/vm-service/node/count")
    public Result getCount(){
       Integer count =  nodeService.getCount();
        return Result.success(count);
    }
    @GetMapping("/api/vm-service/node/nodeCollect")
    public Result getCollectCount(){
        Integer count =  nodeService.getCount();
        return Result.success(count);
    }

}
